package com.mantru.iotpot.setting;

import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.DateClass.Calibration_date;
import com.mantru.iotpot.R;
import com.mantru.iotpot.setting.Adapter.PipelineSelfCleaningAdapter;
import com.mantru.iotpot.setting.Adapter.SeasoningCalibrationAdapter;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PipelineSelfCleaningActivity extends AppCompatActivity {
    private ApplicationClass ac;
    private RecyclerView recyclerView;
    private List<Calibration_date> calibrationDateList = new ArrayList<>();
    private PipelineSelfCleaningAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pipeline_self_cleaning);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        findViewById(R.id.pipeline_self_cleaningback).setOnClickListener(v -> {
            for (int i = 1;i < 10;i++){
                ac.Calibration_Control_Nozzle(i, 0);
                Log.d("PipelineSelfCleaningActivity","关闭料管id：" + i);
            }
            finish();
        });
        recyclerView = findViewById(R.id.pipeline_self_cleaning_recyclerview);
    }

    // 获取Json数据并解析
    private void getJson() {
        String json_data = ac.GetSQLData_Calibration_List();
        Log.d("SeasoningCalibrationActivity", "JSON: " + json_data);
        try {
            // 解析 JSON 数据
            JSONObject jsonResponse = new JSONObject(json_data);
            boolean success = jsonResponse.optBoolean("success");
            if (success) {
                JSONArray itemDataArray = jsonResponse.optJSONArray("data");
                if (itemDataArray != null) {
                    for (int i = 0; i < itemDataArray.length(); i++) {
                        JSONObject itemObject = itemDataArray.optJSONObject(i);
                        if (itemObject != null) {
                            String nozzleId = itemObject.optString("nozzle_id");
                            String nozzleName = itemObject.optString("nozzle_name");
                            String isNeedCalibration = itemObject.optString("is_need_calibration");
                            // 创建 Calibration_date 对象并添加到列表
                            Calibration_date calibrationDate = new Calibration_date(
                                    nozzleId,
                                    nozzleName,
                                    isNeedCalibration
                            );
                            calibrationDateList.add(calibrationDate);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("SeasoningCalibrationActivity", "JSON: 解析失败");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getJson();//获取json数据
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PipelineSelfCleaningAdapter(calibrationDateList,ac,this);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}